/**
 * 
 */
package com.zc.BankCreditManagement.ServiceImpl;

import com.zc.BankCreditManagement.DAO.UserDAO;
import com.zc.BankCreditManagement.JavaBean.User;
import com.zc.BankCreditManagement.Service.UserService;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class UserServiceImpl implements UserService {
	UserDAO uDAO;

	public UserServiceImpl() {
		this.uDAO = new UserDAO();
	}

	/**
	 * 	修改密码
	 * 
	 * @see 当旧密码符合条件时，才可以修改密码
	 */
	public int resetPwdTogether(String userid, String oldPwd, String newPwd,boolean excludeDelete) {
		int count = 0;
		if (isIdConnPWD(userid, oldPwd, excludeDelete) != null) {
			count = this.uDAO.resetPWDById(userid, newPwd);
		} else {
			System.out.println("用户名或密码不匹配，请重试！");
		}
		return count;
	}
	/**
	 * 	修改密码
	 * 
	 * @see 请确认用户权限以及身份验证
	 */
	public int resetPwdWithNewPwd(String userid, String newPwd) {
		
		return this.uDAO.resetPWDById(userid, newPwd);
	}
	/**
	 * 	密码合法性验证
	 * 
	 * @see 长度6-20 不为空
	 */
	public boolean suitableNewPwd(String newPwd) {
		boolean flag = false;
		if(newPwd.length()<6||newPwd.length()>20) {
			System.out.println("您的新密码长度不符合规定（6-20），请重试！");
		}else if(newPwd.equals("")||newPwd==null) {
			System.out.println("新密码不可为空！");
		}else {
			flag = true;
		}
		return flag;
	}

	/**
	 * 	判断用户名密码是否匹配
	 */
	public User isIdConnPWD(String userid, String pwd,boolean excludeDelete) {
		User u = this.uDAO.getUserInfoById(userid, excludeDelete);

		if (u != null) {//查得到
			if (!pwd.equals(u.getPwd())) {//不匹配
				System.out.println("用户名或密码不匹配，请重试！");
			} 
		} else {
			System.out.println("用户名或密码不匹配，请重试！");
		}
		return u;
	}

	public User getUserByPeoId(int peoId) {
		return this.uDAO.getUserInfoById(peoId+"", true);
	}

	/**
	 * 	用户注册
	 */
	public int regiNewUser(User u) {
		int count = 0;
		if (this.uDAO.getUserInfoById(u.getUserId(), true) == null) {
			System.out.println("用户Id可用");
			count = this.uDAO.insertNewUser(u);
		} else {
			System.out.println("用户Id重复，请修改！");
		}
		return count;
	}

	/**
	 * 	获取用户类型/也可以用来判断是否存在该用户
	 */
	public int getUserType(String userId) {
		User u = this.uDAO.getUserInfoById(userId, true);
		if(u!=null) {
			return u.getUserType();
		}else {
			return -1;
		}
	}
	
	/**
	 * 	获取用户类型/也可以用来判断是否存在该用户
	 */
	public int getUserEmail(String userId) {
		User u = this.uDAO.getUserInfoById(userId, true);
		if(u!=null) {
			return u.getUserType();
		}else {
			return -1;
		}
	}
	
}
